package com.AntiThesis.atmodtx2;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.util.EnumParticleTypes;
import net.minecraftforge.fml.common.*;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import net.minecraftforge.fml.common.network.FMLEventChannel;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


@Mod(modid = Atmodtx2.MODID,name = Atmodtx2.NAME,version = Atmodtx2.VERSION,acceptedMinecraftVersions = "1.12.2")
public class Atmodtx2 {

    public static final String MODID = "atmodtx2";
    public static final String NAME = "atmodtx2";
    public static final String VERSION = "1.0";
    private static Logger logger;
    @Mod.Instance(Atmodtx2.MODID)
    public static Atmodtx2 instance;
    @SidedProxy(clientSide = "com.AntiThesis.atmodtx2.common.CommonProxy",
            serverSide = "com.AntiThesis.atmodtx2.client.ClientProxy")
    public static com.AntiThesis.atmodtx2.common.CommonProxy proxy;
    public static FMLEventChannel channel2 = NetworkRegistry.INSTANCE.newEventDrivenChannel("atmodtx");
    public static FMLEventChannel channel = NetworkRegistry.INSTANCE.newEventDrivenChannel("BungeeCord");

    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event) throws IOException {
        System.out.println("bilibili: https://space.bilibili.com/88258917");
        channel.register(this);
        channel2.register(this);
        proxy.preInit(event);
        // System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
		// 
        // ModContainer containerFor = FMLCommonHandler.instance().findContainerFor(this);
        // ModContainer container = containerFor;
        // File source = container.getSource();
        // System.out.println(source.getName());
        // System.out.println(source.getPath());
        // System.out.println(source);
        // JarFile jarFile = new JarFile(source);
		// 
        // Enumeration<JarEntry> entries = jarFile.entries();
        // while (entries.hasMoreElements()){
        //     JarEntry jarEntry = entries.nextElement();
        //     System.out.println(jarEntry.getAttributes());
        //     System.out.println(jarEntry.getCodeSigners());
        //     System.out.println(jarEntry.getCertificates());
        //     System.out.println(jarEntry.getName());
        // }
        // System.out.println("+++++++++++++++++++++++++++++++++++++++++++");
    }


    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {
        proxy.init(event);
    }

    @Mod.EventHandler
    public void postInit(FMLPostInitializationEvent event) {
        proxy.postInit(event);
    }

    @SubscribeEvent
    public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent evt) {
        ByteBuf payload = evt.getPacket().payload();
        StringBuilder builder = new StringBuilder();
        try {
            ByteBufInputStream in = new ByteBufInputStream(payload);
            byte[] buf=new byte[1024];
            int len=0;

            while((len=in.read(buf))!=-1){
                builder.append(new String(buf), 0, len);
            }
            if (builder.toString().startsWith("defalut")){
                String s2= builder.toString().split("defalut")[1];
                String[] s = s2.split(";");

                new AntiThesisSkill2().skill(s);
            }
            else if (builder.toString().startsWith("line")){
                String s2= builder.toString().split("line")[1];
                String[] s = s2.split(";");
                new LineEffect().skill(s);
            }
            else if (builder.toString().startsWith("sphere")){
                String s2= builder.toString().split("sphere")[1];
                String[] s = s2.split(";");
                new ShareEffect().skill(s);
            }


            in.close();
        } catch (Exception e) {
        }
    }

}